<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class manage extends admin {
	private $db;
	public $siteid;
	public $train_level;
	public $train_type;
	public $train_rank;
	public $train_result;
	public $train_school;
	public $train_year;
	
	function __construct() {
	    parent::__construct();
	    pc_base::load_app_class('global');
	    $this->db = pc_base::load_model('s_certificate_model');//加载model
	    $this->admin_db = pc_base::load_model('admin_model');
	    $this->siteid = get_siteid();//获取当前站点
	    $this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
	    $model_setting=pc_base::load_model('site_setting_model');
	    $this->train_level= $model_setting->getValue("train_level",$this->siteid);//获取学级
	    $this->train_type= $model_setting->getValue("train_type",$this->siteid);//获取学类
	    $this->train_rank= $model_setting->getValue("train_rank",$this->siteid);//获取学期
	    $this->train_result= $model_setting->getValue("train_result",$this->siteid);//获取成绩
	    $this->train_school= $model_setting->getValue("train_school",$this->siteid);//获取校区
	    $this->train_year= $model_setting->getValue("train_year",$this->siteid);//获取证书年份
	}
	
	public function init() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where.= " status != 2 and siteid=".$this->siteid;
		if ($_GET['name'] || $_GET['identification']|| $_GET['certificate_number']|| $_GET['train_level']|| $_GET['train_school']) {
			if (!empty($_GET['name'])) {
				$name = $_GET['name'];
				$where.= " and name like '%".$name."%'";
			}
			if ($_GET['certificate_number']) {
				$certificate_number = $_GET['certificate_number'];
				$where.= " and certificate_number = '$certificate_number'";
			}
			if ($_GET['identification']) {
				$identification = $_GET['identification'];
				$where.= " and identification = '$identification'";
			}
			if ($_GET['train_level']) {
				$train_level = $_GET['train_level'];
				$where.= " and train_level = '$train_level'";
			}
			if ($_GET['train_school']) {
				$train_school = $_GET['train_school'];
				$where.= " and train_school = '$train_school'";
			}
			$certificate_info = $this->db->listinfo($where,'id DESC',$page,10);
		} else {
			$certificate_info = $this->db->listinfo($where,'id DESC',$page,10);
		}
		$username = array();
		foreach ($certificate_info as $k=>$v){
			$username = $this->admin_db->get_one(array('userid'=>$v['updated_by']),'realname');
			$certificate_info[$k]['username'] = $username['realname'];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('certificate_list');
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$info = isset($_POST['talent']) ? $_POST['talent'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$data['name'] = $info['name'];
			$data['name_letter'] = $info['name_letter'];
			$data['identification'] = $info['identification'];
			$data['first_letter'] = $info['first_letter'];
			$data['gender'] = $info['gender'];
			$data['train_level'] = $info['train_level'];
			$data['train_type'] = $info['train_type'];
			$data['train_rank'] = $info['train_rank'];
			$data['train_result'] = $info['train_result'];
			$data['train_school'] = $info['train_school'];
			$data['train_year'] = $info['train_year'];
			$data['height'] = $info['height'];
			$data['profession'] = $info['profession'];
			$data['image_home'] = $info['image_home'];
			$data['award_history'] = $info['award_history'];
			$data['contest_history'] = $info['contest_history'];
			$data['perform_history'] = $info['perform_history'];
			$data['thoughts'] = $info['thoughts'];
			$data['start_time'] = $info['start_time'];
			$data['end_time'] = $info['end_time'];
			$data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s',time());
			$data['created_by'] = $data['updated_by'] = $data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$data['status'] = '1';
			$data['is_brilliant'] = $info['thoughts']?1:0;
			switch (array_keys($this->train_school,$info['train_school'])[0]) {
			    case 1:
			     $school='BJ';
			    break;
			    case 2:
			     $school='SZ';
			    break;
			    default:
			     $school='CJ';
			    break;
			}
			switch (array_keys($this->train_type,$info['train_type'])[0]) {
			    case 1:
			     $type='SE';
			    break;
			    case 2:
			     $type='MT';
			    case 3:
			     $type='SX';
			    break;
			    default:
			     $type='CE';
			    break;
			}
			switch ($info['gender']) {
			    case 1:
			     $gender=1;
			    break;
			    case 2:
			     $gender=0;
			    break;
			    default:
			     $gender=1;
			    break;
			}
			if($info['train_year']){
			    $year = substr($info['train_year'], -2);
			}else{
			    $year = date('y',time());
			}
			//查询出该校区目前最大的证书编号
			$number = $this->db->select(' 
id=(select MAX(id) from nsrweb_student_certificate where train_school="'.$info['train_school'].'")','certificate_number');
			//截取后四位，取最大，如果没有则取0001
			$number = intval(substr($number[0]['certificate_number'],-4));
			if(!$number){
			    $number='0001';
			}else{
			    $number = $number+1;
			    if($number<10){
			        $number = '000'.$number;
			    }elseif ($number>=10 && $number<100){
			        $number = '00'.$number;
			    }elseif ($number>=100 && $number<1000){
			        $number = '0'.$number;
			    }
			}
			//按规则拼接证书编号：校区(北京：BJ、深圳：SZ)+学类(少儿培训：SE、成人培训：MT、师资培训：SX)+性别(男：1、女：0)+年度(取年的后两位数，如2017：17)+证书号后四位(根据数据库中查询结果进行判断，如从未该校区的证书发放记录则为0001，其他则在最大的后四位数基础上+1)
			$data['certificate_number'] = $school.$type.$gender.$year.$number;
			$result = $this->db->insert($data);
			$id = $this->db->insert_id();
			if ($result) {
                //记录行为日志
                $log_content = '<span style="color:green;">添加证书</span>[证书id：'.$id.'；学员姓名：'.$info['name'].']';
                $this->manage_admin_log($log_content);
				showmessage(L('add_student_success'), '?m=s_certificate&c=manage&a=init');
			} else {
				showmessage(L('add_student_error'),HTTP_REFERER);
			}
		} else {
			pc_base::load_sys_class('form','',0);
			include $this->admin_tpl('certificate_add');
		}
	}
	
	public function edit(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		if (isset($_POST['dosubmit'])) {
			$up_info = isset($_POST['talent']) ? $_POST['talent'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$data['name'] = $up_info['name'];
			$data['identification'] = $up_info['identification'];
			$data['first_letter'] = $up_info['first_letter'];
			$data['gender'] = $up_info['gender'];
			$data['train_level'] = $up_info['train_level'];
			$data['train_type'] = $up_info['train_type'];
			$data['train_rank'] = $up_info['train_rank'];
			$data['train_result'] = $up_info['train_result'];
			$data['train_school'] = $up_info['train_school'];
			$data['train_year'] = $up_info['train_year'];
			$data['height'] = $up_info['height'];
			$data['profession'] = $up_info['profession'];
			$data['image_home'] = $up_info['image_home'];
			$data['award_history'] = $up_info['award_history'];
			$data['contest_history'] = $up_info['contest_history'];
			$data['perform_history'] = $up_info['perform_history'];
			$data['thoughts'] = $up_info['thoughts'];
			$data['start_time'] = $up_info['start_time'];
			$data['end_time'] = $up_info['end_time'];
			$data['update_time'] = date('Y-m-d H:i:s',time());
			$data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
                //记录行为日志
                $log_content = '<span style="color:blue;">修改证书</span>[证书id：'.$id.'；学员姓名：'.$up_info['name'].']';
                $this->manage_admin_log($log_content);
				showmessage(L('edit_student_success'), '?m=s_certificate&c=manage&a=init');
			} else {
				showmessage(L('edit_student_error'),HTTP_REFERER);
			}
		} else {
			$show_header = false;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('certificate_edit');
		}
	}
	
	public function del(){
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
	            showmessage(L('message not exits'), HTTP_REFERER);
	        }
	        $id = $_REQUEST['id'];
	        $name = $_REQUEST['name'];
	        if (is_array($id)) {
	            foreach ($id as $k => $v) {
	                if (intval($v)) {
	                    $id[$k] = intval($v);
	                } else {
	                    unset($id[$k]);
	                }
	            }
	            $id = implode('\',\'', $id);
	        }
	        $result = $this->db->update(array(
	            'status' => '2'
	        ), "id in ('$id')");
	        if ($result) {
                //记录行为日志
                $log_content = '<span style="color:red;">删除证书</span>[证书id：'.$id.'；学员姓名：'.$name.']';
                $this->manage_admin_log($log_content);
	            showmessage(L('del_student_success'), HTTP_REFERER);
	        } else {
	            showmessage(L('del_student_error'), HTTP_REFERER);
	        }
	}
	
	public function detail() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		$show_header = false;
		pc_base::load_sys_class('form', '', 0);
		include $this->admin_tpl('student_detail');
	}
	
	/**
	* 添加学员校验身份证号，防止学员重复
	* @date: 2017年3月15日 上午9:57:24
	* @author: LIYANG
	*/
	public function check_student() {
		$student_id = $_GET['identification'];
		if (isset($student_id)){
			$id = $this->db->get_one(array('identification'=>$student_id,'is_student'=>1,'siteid'=>$this->siteid));
			if ($id['identification'] == $student_id) {
				exit('0');
			} else {
				exit('1');
			}
		} else {
			exit('0');
		}
	}
}